package com.xnyzc.lhy.finance.controller.bank;

import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.finance.entity.bank.AddBankParams;
import com.xnyzc.lhy.finance.entity.bank.OaDBindBankCard;
import com.xnyzc.lhy.finance.params.dto.bank.OaBankCardBindDTO;
import com.xnyzc.lhy.finance.service.bank.IOaDBankCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 银行卡操作表 前端控制器
 * </p>
 *
 * @author cuixiaojian
 * @since 2019-10-16
 */
@RestController
@RequestMapping("/finance/oa-dbank-card")
public class OaDBankCardController {

    @Autowired
    private IOaDBankCardService iOaDBankCardService;

    /**
     * 新增支持的银行
     *
     * @return List
     */
    @PostMapping("addBank")
    public Rv addBank(@RequestBody AddBankParams params) {
        return iOaDBankCardService.addBank(params);
    }

    /**
     * 更新支持的银行
     *
     * @return List
     */
    @PostMapping("updateBank")
    public Rv updateBank(@RequestBody AddBankParams params) {
        return iOaDBankCardService.updateBank(params);
    }

    /**
     * 获取所有支持的银行
     *
     * @return List
     */
    @GetMapping("getBank")
    public Rv getBank() {
        return iOaDBankCardService.getBank();
    }

    /**
     * 根据ID获取支持的银行
     *
     * @return List
     */
    @GetMapping("getBankById")
    public Rv getBank(@RequestParam("bankId") Long id) {
        return iOaDBankCardService.getBank(id);
    }

    /**
     * 获取司机姓名和身份证号
     *
     * @return Map
     */
    @PostMapping("getDriverNameAndIDEntity")
    public Rv getDriverNameAndIDEntity() {
        Long driverId = TokenUtil.getTokenUserId();
        return iOaDBankCardService.getDriverNameAndIDEntity(driverId);
    }

    /**
     * 绑定银行卡
     *
     * @return Map
     */
    @PostMapping("bindBackCard")
    public Rv bindBackCard(@RequestBody OaBankCardBindDTO oaBankCardBindDTO) {
        oaBankCardBindDTO.setDriverId(TokenUtil.getTokenUserId());
        return iOaDBankCardService.bindBankCard(oaBankCardBindDTO);
    }

    /**
     * 修改银行卡开户行
     */
    @PostMapping("updateBankBatch")
    public Rv updateBankBatch(@RequestBody OaDBindBankCard oaDBindBankCard) {
        return iOaDBankCardService.updateBankBatch(oaDBindBankCard);
    }

    /**
     * 检查银行卡绑定状态
     *
     * @return Map
     */
    @GetMapping("checkBankCardBindStatus")
    public Rv checkBankCardBindStatus() {
        Long driverId = TokenUtil.getTokenUserId();
        return iOaDBankCardService.checkBankCardBindStatus(driverId);
    }

    /**
     * 司机切换银行卡
     *
     * @param cardId 银行卡ID
     * @return Map
     */
    @PostMapping("switchBankCard")
    public Rv switchBankCard(@RequestParam("cardId") Long cardId) {
        Long driverId = TokenUtil.getTokenUserId();
        return iOaDBankCardService.switchBankCard(driverId, cardId);
    }

    /**
     * 司机查询已绑定银行卡列表
     *
     * @return List
     */
    @PostMapping("driverBindBankCardList")
    public Rv driverBindBankCardList() {
        Long driverId = TokenUtil.getTokenUserId();
        return iOaDBankCardService.driverBindBankCardList(driverId);
    }

    /**
     * 我的银行卡
     */
    @GetMapping("myCard")
    public Rv myCard() {
        return iOaDBankCardService.myCard();
    }

    /**
     * 解绑银行卡
     *
     * @param oaDBindBankCard 关系ID
     * @return
     */
    @PostMapping("uniteBindCard")
    public Rv untieBindBank(
            @RequestBody OaDBindBankCard oaDBindBankCard
    ) {

        if (CheckUtil.objIsEmpty(oaDBindBankCard)) {
            throw PangException.create(EErrorCode.missingArg);
        }

        return iOaDBankCardService.untieBindBank(oaDBindBankCard.getBankCardId());
    }

}
